
#include "sc8701.h"

#define DUTY_MAX        (1000)

void sc8701_init(sc8701_t *t, uint16_t v_max, uint16_t i_max, int16_t v_offset, int16_t i_offset)
{
    t->i_max = i_max;
    t->v_max = v_max;
    t->i_offset = i_offset;
    t->v_offset = v_offset;
    t->bus->init(t->bus_handle, DUTY_MAX);
    sc8701_enable(t, 0);
    sc8701_set_u(t, 0);
    sc8701_set_i(t, 0);
}

void sc8701_enable(sc8701_t *t, uint8_t enable)
{
    t->enable = enable;
    t->bus->enable(t->bus_handle, t->enable);
}

void sc8701_set_u(sc8701_t *t, uint16_t value)
{
    int32_t v2 = value;
    t->v = value;
    v2 = v2<t->v_offset ? 0: v2-t->v_offset;
    t->bus->set_u_duty(t->bus_handle, v2*DUTY_MAX/t->v_max);
}

void sc8701_set_i(sc8701_t *t, uint16_t value)
{
    int32_t v2 = value;
    t->i = value;
    v2 = v2<t->v_offset ? 0: v2-t->i_offset;
    t->bus->set_i_duty(t->bus_handle, v2*DUTY_MAX/t->i_max);
    
}
